//
//  RootViewController.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/29.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()
{
    UILabel *_titles;
}

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.navigationController.navigationBar setBarTintColor:RGB(255, 255, 255)];
//    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)setupTitleView:(NSString *)titleStr {
    if (_titles) {
        _titles.text = titleStr;
        return;
    }
    _titles = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    _titles.text = titleStr;
    _titles.textAlignment = NSTextAlignmentCenter;
    _titles.textColor = RGB(25, 25, 25);
    _titles.font = [UIFont fontWithName:@"PingFang SC" size:18];
    self.navigationItem.titleView = _titles;
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)leftButton:(NSString *)title {
    if(self.navigationItem == nil) {
        return;
    }
    // 初始化
    CGFloat imgVWidth = 10.0;
    CGFloat imgVHeight = 18.f;
    CGFloat btnHeight = 44.0f;
    UIImageView * imgView = [self leftImageView];
    
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    imgView.frame = CGRectMake(0, (btnHeight-imgVHeight)/2, imgVWidth, imgVHeight);
    _leftButton.frame = CGRectMake(0, 0, imgVWidth, btnHeight);
    _leftButton.userInteractionEnabled = YES;
    
    // 填充数据
    [_leftButton addSubview:imgView];
    [_leftButton addTarget:self action:@selector(leftBarItemOnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [_leftButton addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftBarItemOnClick:)]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftButton];
    [self leftButtonDisplayState];
}

- (void)leftButtonDisplayState {        // 不可现函数
    if( [self.navigationController.viewControllers count] < 1) {
        _leftButton.hidden = YES;
    }
    else {
        _leftButton.hidden = NO;
    }
}

- (UIImageView *)leftImageView
{
    return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"返回"]] ;
}

- (void)leftBarItemOnClick:(id)sender {
    if(self.navigationController && [self.navigationController.viewControllers count] > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

//----------------------rightButton---------------------------
- (void)rightButton {
    if(self.navigationItem == nil) {
        return;
    }
    // 初始化
    CGFloat imgVWidth = 24.0;
    CGFloat imgVHeight = 24.f;
    CGFloat btnHeight = 44.0f;
    UIImageView * imgView = [self rightImageView];
    
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    imgView.frame = CGRectMake(0, (btnHeight-imgVHeight)/2, imgVWidth, imgVHeight);
    _rightButton.frame = CGRectMake(0, 0, imgVWidth, btnHeight);
    _rightButton.userInteractionEnabled = YES;
    
    // 填充数据
    [_rightButton addSubview:imgView];
    [_rightButton addTarget:self action:@selector(rightBarItemOnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
    [self rightButtonDisplayState];
}

- (void)rightButtonDisplayState {        // 不可现函数
    if( [self.navigationController.viewControllers count] < 1) {
        _rightButton.hidden = YES;
    }
    else {
        _rightButton.hidden = NO;
    }
}

- (UIImageView *)rightImageView
{
    return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"说明"]] ;
}

- (void)rightBarItemOnClick:(id)sender {
    
}
@end
